package ru.compscicenter.uno.model;

import ru.compscicenter.uno.controller.ServerController;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * User: SLeeP
 * Date: 04.11.11
 * Time: 19:21
 */
public class StringRepresentation {
    private static final String CARD_SEPARATOR = ":";
    private static final String PLAYER_SEPARATOR = "!";
    private StringRepresentation() {}

    public static String getStringRepresentation(Card card) {
        return card.getId() + CARD_SEPARATOR + card.getType() + CARD_SEPARATOR + card.getColor();
    }

    public static String getStringRepresentation(Rules rules) {
        String representation = "pointsToWin " + rules.getPointsToWin() + " ";
        for (Rules.Rule rule : Rules.Rule.values()) {
            representation += rule.name() + " " + rules.isActivated(rule) + " ";
        }
        return representation;
    }

    public static Rules rulesFromStringRepresentation(String representation) {
        Rules rules = new Rules();
        String[] arr = representation.split(" ");
        rules.setPointsToWin(Integer.parseInt(arr[1]));
        for (int i = 2; i != arr.length; i += 2) {
            rules.set(Rules.Rule.valueOf(arr[i]), Boolean.parseBoolean(arr[i+1]));
        }

        return rules;
    }

    public static String getStringRepresentation(Player player) {
        String result = player.getName() + " " + player.getId() + " " + player.getPoints();
        for (Card card : player.getCards()) {
            result += " " + getStringRepresentation(card);
        }
        return result;
    }

    public static String getStringRepresentation(int playerID, Model model) {
        String result = ServerController.MODEL_DATA + " " + playerID + " " + getStringRepresentation(model.getTopCard()) + " " + ServerController.DECK;

        for (int i = 0; i != model.getTakingDeckSize(); i++) {
            result += " " + getStringRepresentation(model.getTakingDeck().get(i));
        }

        result += " " + ServerController.PLAYERS;

        for (int i = 0; i != model.getPlayersCount(); i++) {
            result += PLAYER_SEPARATOR + getStringRepresentation(model.getPlayers().get(i));
        }

        return result;
    }

    public static Card cardFromStringRepresentation(String representation) {
        String[] cardParts = representation.split(CARD_SEPARATOR);
        return new Card(Integer.parseInt(cardParts[0]), Card.Type.valueOf(cardParts[1]), Card.Color.valueOf(cardParts[2]));
    }

    public static Player playerFromStringRepresentation(String representation) {
        String[] player = representation.split(" ");
        String name = player[0];
        int id = Integer.parseInt(player[1]);
        int points = Integer.parseInt(player[2]);
        List<Card> cards = Collections.synchronizedList(new ArrayList<Card>());

        for (int j = 3; j != player.length; j++) {
            cards.add(cardFromStringRepresentation(player[j]));
        }

        Player playerInstance = new Player(name, id, cards);
        playerInstance.setPoints(points);
        return playerInstance;
    }

    public static Model modelFromStringRepresentation(String representation) {
        Model model = new Model();
        int pos = representation.indexOf(ServerController.PLAYERS);
        String[] deckParts = representation.substring(0, pos).split(" ");
        String[] playersParts = representation.substring(pos).split(PLAYER_SEPARATOR);

        model.setMyPlayerId(Integer.parseInt(deckParts[0]));
        Card top = cardFromStringRepresentation(deckParts[1]);
        model.setTopCard(top);

        for (int i = 3; i != deckParts.length; i++) {
            model.getTakingDeck().add(cardFromStringRepresentation(deckParts[i]));
        }

        for (int i = 1; i != playersParts.length; i++) {
            model.getPlayers().add(playerFromStringRepresentation(playersParts[i]));
        }

        return model;
    }
}
